# -*- coding: utf-8 -*-
import uuid

from sqlalchemy import Column, Integer, String, Boolean, SmallInteger, ForeignKey, Text, Float, Index

from db import Base
from db.models.base import GUID, BaseDatabaseModel, ARRAY


class ClauseStandard(Base, BaseDatabaseModel):
    __tablename__ = "clause_standard"

    id = Column(GUID(), primary_key=True, index=True, default=uuid.uuid1)
    line_number = Column(Integer, nullable=False, index=True, comment="行号")
    clause_name = Column(String(512), nullable=True, default=None, index=True, comment="条款类型")
    institution = Column(String(512), nullable=True, default=None, index=True, comment="制度要素")
    clause_number = Column(String(512), index=True, comment="法规条款")
    original_text = Column(Text, comment="法规原文")
    artificial_text = Column(Text, comment="人工优化过的内容")
    area = Column(String(512), index=True, default="", comment="适用范围")
    cargo = Column(String(512), index=True, default="", comment="普货危货")
    is_delete = Column(Boolean, index=True, default=False)

    user = Column(Integer, ForeignKey('users.id'))
    file_id = Column(GUID(), ForeignKey('clause_files.id'), comment="文件id")

    __table_args__ = (
        Index('idx_original_text', 'original_text', mysql_length=300),
        Index('idx_artificial_text', 'artificial_text', mysql_length=300)
    )

    def to_json(self):
        return {
            "id": str(self.id),
            "line_number": self.line_number,
            "clause_name": self.clause_name,
            "institution": self.institution,
            "clause_number": self.clause_number,
            "original_text": self.original_text,
            "artificial_text": self.artificial_text,
            "area": self.area,
            "cargo": self.cargo if self.cargo else "",
            "file_id": str(self.file_id),
            "updated_time": self.updated_time.strftime("%Y-%d-%m %H:%M:%S"),
        }


class ClauseFile(Base, BaseDatabaseModel):
    __tablename__ = "clause_files"

    id = Column(GUID(), primary_key=True, index=True, default=uuid.uuid1)
    created_by = Column(Integer, ForeignKey('users.id'), comment="创建人")
    name = Column(String(255), nullable=True, comment="md5文件名")
    origin_name = Column(String(255), nullable=True, comment="原始文件名")
    location = Column(String(512), nullable=True, comment="存储位置")
    type = Column(String(32), nullable=True, comment="文件类型")
    size = Column(Float, nullable=True, comment="文件大小")
    clause_type = Column(SmallInteger, default=0, index=True, comment="0:标准条款文件, 1:新条款文件")

    is_delete = Column(Boolean, default=False, index=True)
    status = Column(SmallInteger, nullable=True, comment="0:no_parsed, 1:parsed", default=0, index=True)

    def to_json(self):
        return {
            "name": self.origin_name,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "id": str(self.id),
            "file_type": self.type,
            "clause_type": self.clause_type,
            "size": self.size,
            "status": self.status,
            "updated_time": self.updated_time.strftime("%Y-%m-%d %H:%M:%S"),
        }


class ClauseCompareHistory(Base, BaseDatabaseModel):
    __tablename__ = "clause_compare_history"

    id = Column(GUID(), primary_key=True, index=True, default=uuid.uuid1)
    name = Column(String(255), nullable=True, comment="名称")
    file_hash_name = Column(String(255), nullable=True, comment="文件hash+后缀")
    parsed_docs = Column(ARRAY, default=[], nullable=True, comment="文件解析结果")
    compare_result = Column(ARRAY, nullable=True, default=[], comment="存储对比结果")
    created_by = Column(Integer, ForeignKey('users.id'), comment="创建人")
    is_delete = Column(Boolean, default=False, index=True)
    clause = Column(GUID(), ForeignKey('clause_files.id'))

    def to_json(self):
        return {
            "id": str(self.id),
            "name": self.name,
            "compare_result": self.compare_result,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "updated_time": self.updated_time.strftime("%Y-%m-%d %H:%M:%S"),
        }


class ClauseGroups(Base):
    __tablename__ = "clause_group"
    id = Column(Integer, primary_key=True, index=True)
    clause = Column(GUID(), ForeignKey('clause_files.id'))
    group = Column(Integer, ForeignKey('groups.id'))
